--
-- Author: shunguo.chen
-- Date: 2018-10-04 15:41:09
--
-- LotteryPanel 十连抽

local ResRender = require("app.widget.renders.ResRender")

local ClassRef = app.mvc.createPanelClass(...)

local BUY_TIMES_STR = L("demondShop_extrapool_tips")
local TIME_STR = L("demondShop_freepool_time")
local FREE_STR = L("demondShop_freepool_tips")
local NEED_BUY_ITEM_STR = L("mountticket_notenough")

function ClassRef.create(node)
	local sprite = ClassRef.extend(node)
	sprite:init()
	return sprite
end

function ClassRef:init(data)
	self.super.init(self, data)

	self._resRender = ResRender.create(self._ui.Panel_money)
	self._resRender:setData({type = Enums.TipsType.item, id = Constant.Lottery_ItemId, num = 0})

	self._ui.Button_1:addClickEventListener(function()
		if self._data == nil then
			return
		end
		local isFree = (self._data:getFreeTime() <= 0)
		if isFree then
			executeFunc(self._onOperate, "lottery", 1)
		else
			local have = self._data:getHaveNum()
			local need = self._data.button1Cost:getAmount()
			if need > have then
				local str = string.format(NEED_BUY_ITEM_STR, self._data.button1Cost:getName(true))
				executeFunc(self._onOperate, "needBuy", str)
			else
				executeFunc(self._onOperate, "lottery", 1)
			end
		end
	end)
	self._ui.Button_2:addClickEventListener(function()
		if self._data == nil then
			return
		end
		local have = self._data:getHaveNum()
		local need = self._data.button2Cost:getAmount()
		if need > have then
			local str = string.format(NEED_BUY_ITEM_STR, self._data.button2Cost:getName(true))
			executeFunc(self._onOperate, "needBuy", str)
		else
			executeFunc(self._onOperate, "lottery", 2)
		end
	end)

	self._lotteryOnePriceText = self._ui.Button_1:findChild("num_1/num")
	self._lotteryTenPriceText = self._ui.Button_2:findChild("num_2/num")
	self._lotteryFreeTag = self._ui.Button_1:getChildByName("dot")

	if app:isIOS() then
		self._ui.tips:setVisible(true)
	else
		self._ui.tips:setVisible(false)
	end

    -- drop button
    self._ui.btn_see:addClickEventListener(function()
        executeFunc(self._onOperate, "openLotteryDropView")
    end)

	-- for test
	local panel = self._ui.Panel
	panel:getChildByName("text"):setString("text")
	panel:getChildByName("text_tip"):setString("text_tip")
end

function ClassRef:onOperate(cb)
	self._onOperate = cb
end

function ClassRef:getGuideNode(id)
	if not self:isVisible() then return end

	if id == 420 then
		-- 十连抽按钮
		local have = self._data:getHaveNum()
		local need = self._data.button2Cost:getAmount()
		if need <= have then
			return self._ui.Button_2
		end
	elseif id == 442 then
		-- 单抽按钮
		return self._ui.Button_1
	end
end

-- data = LotteryInfo
function ClassRef:refresh()
	local data = self._data
	if not data then
		release_print("==============>>> LotteryPanel refresh data is nil")
		return
	end
	self._lotteryOnePriceText:setString(data.button1Cost:getAmount())
	local parent = self._lotteryOnePriceText:getParent()
	local icon = parent:getChildByName("gold")
	display.setNodesMiddle(parent, nil, icon, self._lotteryOnePriceText)

	self._lotteryTenPriceText:setString(data.button2Cost:getAmount())
	local parent = self._lotteryTenPriceText:getParent()
	local icon = parent:getChildByName("gold")
	display.setNodesMiddle(parent, nil, icon, self._lotteryTenPriceText)

	self._ui.text:setString(string.format(BUY_TIMES_STR, data.nextGiftCount))

	self:_refreshTime()
end

function ClassRef:_refreshTime()
	if self._delayAct then
		self:stopAction(self._delayAct)
		self._delayAct = nil
	end

	local freeTime = self._data:getFreeTime()
	if freeTime <= 0 then
		self._ui.text_tip:setString(FREE_STR)
		self._lotteryOnePriceText:setString(0)
		local parent = self._lotteryOnePriceText:getParent()
		local icon = parent:getChildByName("gold")
		display.setNodesMiddle(parent, nil, icon, self._lotteryOnePriceText)
	else
		local timeStr = TimeUtil.secondsToTime(freeTime)
		self._ui.text_tip:setString(string.format(TIME_STR, timeStr))

		self._delayAct = self:performWithDelay(function()
				self._delayAct = nil
				self:_refreshTime()
			end, 1)
	end
end

function ClassRef:updateCoin(num)
	self._resRender:setNum(num)
end

function ClassRef:updateNew(b)
	self._lotteryFreeTag:setVisible(b)
end

return ClassRef